<template>
  <div>
    <div class="login">
      <div class="box">
        <h2>萌宠医族后台管理系统</h2>
        <div class="loginForm">
          <!--status-icon   是否在输入框中显示校验结果反馈图标
						label-width="100px"  表单域标签的宽度
						model	表单数据对象
						rules	表单验证规则
					 -->
          <el-form
            ref="adminForm"
            :model="adminForm"
            :rules="adminRules"
            status-icon
            label-width="100px"
            class="demo-ruleForm"
          >
            <!-- prop	表单域 model 字段 -->
            <el-form-item prop="aname">
              <!--图标 -->
              <i class="el-icon-user"></i>
              <!-- 管理员账号 -->
              <el-input v-model="adminForm.aname" placeholder="账号"></el-input>
            </el-form-item>
            <el-form-item prop="apwd">
              <div
                @click="showPass = !showPass"
                style="float: left; cursor: pointer"
              >
                <i class="el-icon-lock" v-show="!showPass"></i>
                <i class="el-icon-unlock" v-show="showPass"></i>
              </div>
              <div style="float: left">
                <el-input
                  @keyup.enter.native="login"
                  type="password"
                  v-model="adminForm.apwd"
                  placeholder="密码"
                  v-show="!showPass"
                ></el-input>
                <el-input
                  type="text"
                  v-model="adminForm.apwd"
                  placeholder="密码"
                  autocomplete="off"
                  v-show="showPass"
                ></el-input>
              </div>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="login">登录</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { adminLoginServe } from "../utils/service/admin";
export default {
  data() {
    return {
      showPass: false,
      adminForm: {
        aname: "",
        apwd: "",
      },
      adminRules: {
        aname: [
          { required: true, message: "账号不能为空", trigger: "blur" },
          {
            pattern: /^\w{5,10}$/,
            message: "6~10位字符，包含：数字/字母/下划线",
            trigger: "blur",
          },
        ],
        apwd: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 4, max: 12, message: "密码长度为4-12位", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    login() {
      adminLoginServe(this.adminForm.aname, this.adminForm.apwd)
        .then((res) => {
          if (res.code == 200) {
            this.$message({ type: "success", message: "登录成功" });
            sessionStorage["adminname"] = this.adminForm.aname;
            this.$router.push("/admin/index");
          } else {
            this.$message({ type: "error", message: "登录失败" });
          }
        })
        .catch((err) => {
          console.log(err);
          this.$message({ type: "error", message: "网络错误" });
        });
    },
  },
};
</script>

<style scoped>
.login {
  width: 100%;
  height: 100%;
  /*背景图片*/
  background-image: url("../assets/image/login_bg.jpg");
  position: fixed;
  background-size: cover;
}

.box {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  width: 500px;
  height: 300px;
  margin: auto;
  border: 1px solid #316ed6;
  background-color: rgba(0, 0, 0, 0.2);
}

.box h2 {
  padding: 20px 0 20px 0;
  font-size: 28px;
  font-weight: 500;
  text-align: center;
  line-height: 30px;
  color: aliceblue;
}

.el-form .el-form-item i {
  width: 50px;
  color: aliceblue;
}

.el-form .el-form-item .el-input {
  width: 250px;
}

.el-form .el-form-item .el-button {
  width: 300px;
  background: #505458;
}
</style>
